//
// SoapTrace.Net - A soap message monitoring utility. 
//
// Copyright (C) 2005 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)

using NUnit.Framework;
using SoapTrace.Core.Communication;
using SoapTrace.Core.Diagnostics;
using System;
using System.IO;
using System.Text;

namespace SoapTrace.Tests.TcpPacketMonitoring
{
	/// <summary>
	/// Tests that the <see cref="TcpPacketMonitor"/> class
	/// generates an event when data is sent from the client to the server.
	/// </summary>
	[ TestFixture ]
	public class ClientSendsDataTestFixture
	{
		TcpPacketMonitor Monitor;
		int BytesTransferredEvent;
		TcpCommunicationEventArgs EventArgsReceived;
		
		/// <summary>
		/// Initialises each test.
		/// </summary>
		[ TestFixtureSetUp ]
		public void Init( )
		{
			BytesTransferredEvent = 0;
			Monitor = new TcpPacketMonitor( );
			Monitor.BytesTransferred += new TcpCommunicationEventHandler( OnBytesTransferred );
			
			StringReader Reader = new StringReader( GetPacketXml( ) );
			PacketLogEntryCollection LogEntries = PacketLogFile.Load( Reader );
			
			foreach( PacketLogEntry Entry in LogEntries )
			{
				Monitor.ProcessPacket( Entry.Packet );
			}
		}
		
		/// <summary>
		/// Checks that the bytes transferred event was triggered.
		/// </summary>
		[ Test ]
		public void ReceivedSingleBytesTransferredEvent( )
		{
			Assert.AreEqual( 1, BytesTransferredEvent, "Did not receive one bytes transferred event." );
		}
		
		/// <summary>
		/// Check the connection source IP address is correct.
		/// </summary>
		[ Test ]
		public void SourceIPAddress( )
		{
			Assert.AreEqual( "192.168.1.3:1692", EventArgsReceived.SourceAddress.ToString( ),
			                "Unexpected source IP address." );
		}
		
		/// <summary>
		/// Check the connection destination IP address is correct.
		/// </summary>
		[ Test ]
		public void DestinationIPAddress( )
		{
			Assert.AreEqual( "209.171.52.99:80", EventArgsReceived.DestinationAddress.ToString( ),
			                "Unexpected destination IP address." );
		}	
		
		/// <summary>
		/// Check we received the correct message.
		/// </summary>
		[ Test ]
		public void BytesTransferred( )
		{
			string ExpectedMessage = "POST /webservices/latest.asmx HTTP/1.1\r\n" +
				"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 1.1.4322.573)\r\n" +
				"Content-Type: text/xml; charset=utf-8\r\n" +
				"SOAPAction: \"http://codeproject.com/webservices/GetMaxCommentListLength\"\r\n" +
				"Content-Length: 317\r\n" +
				"Expect: 100-continue\r\n" +
				"Connection: Keep-Alive\r\n" +
				"Host: www.codeproject.com\r\n" +
				"\r\n";
			
			string ActualMessage = UnicodeEncoding.UTF8.GetString( EventArgsReceived.GetBytes( ) );
			
			Assert.AreEqual( ExpectedMessage, ActualMessage, "Unexpected message received." );
		}
		
		private void OnBytesTransferred( object sender, TcpCommunicationEventArgs e )
		{
			++BytesTransferredEvent;
			EventArgsReceived = e;
		}
		
		/// <summary>
		/// Gets the xml to be read in by the <see cref="PacketLogFile"/>
		/// class.
		/// </summary>
		private string GetPacketXml( )
		{
			return "<Packets>\r\n" +
					"  <Packet Time=\"2005-01-31 18:49:34.77\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
					"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 00 30 4B 02 40 00 80 06 E8 0B C0 A8 01 03 D1 AB 34 63 06 9C 00 50 BD AA AB 7F 00 00 00 00 70 02 FF FF 4B 4F 00 00 02 04 05 B4 01 01 04 02 </Bytes>\r\n" +
					"  </Packet>\r\n" +
					"  <Packet Time=\"2005-01-31 18:49:34.83\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
					"    <Bytes>00 11 F5 0B 1F 6A 00 11 F5 0B 1F 69 08 00 45 00 00 30 1A EA 40 00 67 06 31 24 D1 AB 34 63 C0 A8 01 03 00 50 06 9C 6F C1 EF C4 BD AA AB 80 70 12 FF FF EB DF 00 00 02 04 05 8C 01 01 04 02 </Bytes>\r\n" +
					"  </Packet>\r\n" +
					"  <Packet Time=\"2005-01-31 18:49:34.88\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
					"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 00 28 4B 03 40 00 80 06 E8 12 C0 A8 01 03 D1 AB 34 63 06 9C 00 50 BD AA AB 80 6F C1 EF C5 50 10 FF FF 18 7C 00 00 </Bytes>\r\n" +
					"  </Packet>\r\n" +
					"  <Packet Time=\"2005-01-31 18:49:34.93\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
					"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 01 7F 4B 04 40 00 80 06 E6 BA C0 A8 01 03 D1 AB 34 63 06 9C 00 50 BD AA AB 80 6F C1 EF C5 50 18 FF FF F2 8F 00 00 50 4F 53 54 20 2F 77 65 62 73 65 72 76 69 63 65 73 2F 6C 61 74 65 73 74 2E 61 73 6D 78 20 48 54 54 50 2F 31 2E 31 0D 0A 55 73 65 72 2D 41 67 65 6E 74 3A 20 4D 6F 7A 69 6C 6C 61 2F 34 2E 30 20 28 63 6F 6D 70 61 74 69 62 6C 65 3B 20 4D 53 49 45 20 36 2E 30 3B 20 4D 53 20 57 65 62 20 53 65 72 76 69 63 65 73 20 43 6C 69 65 6E 74 20 50 72 6F 74 6F 63 6F 6C 20 31 2E 31 2E 34 33 32 32 2E 35 37 33 29 0D 0A 43 6F 6E 74 65 6E 74 2D 54 79 70 65 3A 20 74 65 78 74 2F 78 6D 6C 3B 20 63 68 61 72 73 65 74 3D 75 74 66 2D 38 0D 0A 53 4F 41 50 41 63 74 69 6F 6E 3A 20 22 68 74 74 70 3A 2F 2F 63 6F 64 65 70 72 6F 6A 65 63 74 2E 63 6F 6D 2F 77 65 62 73 65 72 76 69 63 65 73 2F 47 65 74 4D 61 78 43 6F 6D 6D 65 6E 74 4C 69 73 74 4C 65 6E 67 74 68 22 0D 0A 43 6F 6E 74 65 6E 74 2D 4C 65 6E 67 74 68 3A 20 33 31 37 0D 0A 45 78 70 65 63 74 3A 20 31 30 30 2D 63 6F 6E 74 69 6E 75 65 0D 0A 43 6F 6E 6E 65 63 74 69 6F 6E 3A 20 4B 65 65 70 2D 41 6C 69 76 65 0D 0A 48 6F 73 74 3A 20 77 77 77 2E 63 6F 64 65 70 72 6F 6A 65 63 74 2E 63 6F 6D 0D 0A 0D 0A </Bytes>\r\n" +
					"  </Packet>\r\n" +
					"</Packets>";
		}
	}
}
